package win.zxshuo.www.controller.apis;


import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import win.zxshuo.www.controller.responses.BaseResponse;
import win.zxshuo.www.controller.responses.BooksResponse;
import win.zxshuo.www.model.BookModel;
import win.zxshuo.www.model.chapters.ChapterModel;
import win.zxshuo.www.service.BookService;
import win.zxshuo.www.service.ChapterService;
import win.zxshuo.www.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/chapter")
public class ChapterApiController {

    Log logger = LogFactory.getLog(getClass());

    @Autowired
    ChapterService chapterService;

    @Autowired
    BookService bookService;

    @RequestMapping("/{bookid}/{chapterid}")
    public BaseResponse getChapterContent(@PathVariable Long bookid,@PathVariable Long chapterid){
        BaseResponse baseResponse = new BaseResponse();
        Map<String,Object> data = new HashMap<>();
        try {
            ChapterModel chapterModel = chapterService.getBy(bookid,chapterid);
            String currentChapterContent = "";
            Integer wordNum = 0;
            Long previousChapterId = -1L;
            Long nextChapterId = -1L;
            String chapterTitle = "";
            if (chapterModel.getContent() != null){
                currentChapterContent = chapterModel.getContent();
                wordNum = chapterModel.getChapterNum();
                ChapterModel nextChapter = chapterService.getNextChapter(chapterid,bookid);
                if (nextChapter != null){
                    nextChapterId = nextChapter.getId();
                }
                ChapterModel previousChapter =  chapterService.getPreviousChapter(chapterid,bookid);
                if (previousChapter != null){
                    previousChapterId = previousChapter.getId();
                }
                chapterTitle = chapterModel.getTitle();
            }
            data.put("currentChapterContent",StringUtils.parsePtoList(currentChapterContent));
            data.put("wordNum",wordNum);
            data.put("previousChapterId",previousChapterId);
            data.put("nextChapterId",nextChapterId);
            data.put("chapterTitle",chapterTitle);
            baseResponse.setData(data);
            baseResponse.setTrue();
        }catch (Exception e){
            logger.error(e.getCause());
            logger.info("获取书籍章节出现错误");
            baseResponse.setErrorMsg(e.getMessage());
        }
        return baseResponse;
    }

    @GetMapping("/getchapter/{bookid}")
    public BooksResponse getBookIndex(@PathVariable Long bookid){
        BookModel bookModel = bookService.getBookById(bookid);
        List<ChapterModel> chapterModels = chapterService.getAllChaptersBy(bookModel);
        return BooksResponse.getBookIndex(chapterModels);
    }




}
